<!-- MENU-LOCATION=NONE -->
<!DOCTYPE HTML SYSTEM "-//Macromedia//DWExtension layout-engine 5.0//dialog">
<HTML>
<HEAD>
<!-- Copyright 2009, Alexei White. All rights reserved. -->
<Title>Configure DreamUnit Tests</Title>
<link rel="stylesheet" type="text/css" href="../Shared/du/css/main.css" />
<script type="text/javascript" src="../Shared/du/js/head.js"></script>
<script type="text/javascript" src="../Shared/du/js/ws.js"></script>
<script type="text/javascript" src="../Shared/du/js/remoteData.js"></script>
<script type="text/javascript" src="../Shared/du/js/uid.js"></script>
<script type="text/javascript" src="../Shared/du/js/user.js"></script>
<script type="text/javascript" src="../Shared/du/js/sets.js"></script>
<script type="text/javascript" src="../Shared/du/js/tests.js"></script>
<script type="text/javascript" src="../Shared/du/js4/filetree.js"></script>
<script type="text/javascript" src="../Shared/du/js/prefs.js"></script>

<script type="text/javascript">
function windowDimensions() {
	return "800,595";
}
function commandButtons() { 
	//return new Array("Close", "window.close()"); 
	return new Array();
}
function populateSiteList() {
	var sCh = document.getElementById('siteChooser1');
	var sB = "<p>Dreamweaver Site: <select name=\"sChooser\" id=\"sChooser\" class=\"bigBox\" style=\"width:300px\">";
	var sList = site.getSites();
	var count = 0;
	for (var item in sList) {
		var cSel = "";
		if (count == 0)
			cSel = "selected";
		sB += "<option " + cSel + ">" + sList[item] + "</option>";
		count++;
	}
	
	sB += "</select></p>";
	sCh.innerHTML = sB;
	
	var sCh2 = document.getElementById('siteChooser2');
	var sB = "<p>Dreamweaver Site: <select name=\"sChooser2\" id=\"sChooser2\" class=\"bigBox\" style=\"width:300px\">";
	var sList = site.getSites();
	var count = 0;
	for (var item in sList) {
		var cSel = "";
		if (count == 0)
			cSel = "selected";
		sB += "<option " + cSel + ">" + sList[item] + "</option>";
		count++;
	}
	
	sB += "</select></p>";
	sCh2.innerHTML = sB;	
}
function init() {
	user.init();
	sets.init();
	populateSiteList();
	homeScreen();
}
function homeScreen() {
	renderSetsList();
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "none";
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";	
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";	
	var imArea = document.getElementById("importNewArea");
	imArea.style.display = "none";	
	var simArea = document.getElementById("importSetNotExist");
	simArea.style.display = "none";	
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "block";	

}
function createNewWizard() {
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "none";
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";	
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";	
	var imArea = document.getElementById("importNewArea");
	imArea.style.display = "none";	
	var simArea = document.getElementById("importSetNotExist");
	simArea.style.display = "none";	
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "block";	

}
function importWizard() {
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "none";
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";	
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";	
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "none";	
	var simArea = document.getElementById("importSetNotExist");
	simArea.style.display = "none";	
	var imArea = document.getElementById("importNewArea");
	imArea.style.display = "block";
}
function showLoading() {
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "none";
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "none";
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";		
	var imArea = document.getElementById("importNewArea");
	imArea.style.display = "none";
	var simArea = document.getElementById("importSetNotExist");
	simArea.style.display = "none";	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "block";	
}
function createNewSet() {
	var setnm = document.getElementById("setname");
	setnm.blur();
	var setnms = document.getElementById("sChooser");
	setnms.focus();
	setTimeout("_createNewSet()", 100);
}

function importSet() {
	var setnm = document.getElementById("setname2");
	setnm.blur();
	var setnmi = document.getElementById("setidimport");
	setnmi.blur();	
	var setnms = document.getElementById("sChooser2");
	setnms.focus();
	setTimeout("_importSet()", 100);
}

function _importSet() {
	var setnm = document.getElementById("setname2").value;
	var setid = document.getElementById("setidimport").value;	
	var sitenmObj = document.getElementById("sChooser2");
	var sitenm = sitenmObj.options[sitenmObj.selectedIndex].text;
	showLoading();
	setTimeout("checkImportTestExists('" + setid + "', '" + setnm.replace("'", "").replace("'", "") + "', '" + sitenm.replace("'", "").replace("'", "") + "');", 100);
	//setTimeout("_finalizeNewSet('" + setnm.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "', '" + sitenm.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "')", 10);
}

function checkImportTestExists(uid, setnm, sitenm) {
	var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.SETEXISTS, {'uid':uid}));
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";	
	if (result == true) {
		// Create the test and import it.
		var setOBJ = sets.createNew(user.uid, setnm, sitenm);
		tests.deserializeTestsFromWeb(uid);
		tests.serializeAndWriteTestsToFileSystem(uid);
		homeScreen();		
		renderSet(uid);
	} else {
		// display did not work
		//importSetNotExist
	
		var simArea = document.getElementById("importSetNotExist");
		var res = "<h1>That Set Could Not be Found.</h1>";
		res += "<p>We couldn't find that set. Are you connected to the Internet? Did you mistype the set or include extra spaces in the ID?</p>";
		res += "<input type=\"button\" class=\"maskedButton\" onClick=\"homeScreen()\" value=\"OK\"><br>";
		simArea.innerHTML = res;
		simArea.style.display = "block";
		
	}
}

function _createNewSet() {
	var setnm = document.getElementById("setname").value;
	var sitenmObj = document.getElementById("sChooser");
	var sitenm = sitenmObj.options[sitenmObj.selectedIndex].text;
	showLoading();
	setTimeout("_finalizeNewSet('" + setnm.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "', '" + sitenm.replace(/\\/g, "\\\\").replace(/'/g, "\\'") + "')", 10);
	
}
function _finalizeNewSet(setnm, sitenm) {
	sets.createNew(user.uid, setnm, sitenm);
	homeScreen();
}
function renderSetsList() {
	var res = "";
	for (var i = 0; i < sets.list.length; i++) {
		//res += "<button onClick=\"loadSet('" + sets.list[i].uid + "')\">" + sets.list[i].name + "</button><br>";
		if (sets.list[i].name) {
			res += "<input type=\"button\" class=\"maskedButton\" onClick=\"loadSet('" + sets.list[i].uid + "')\" value=\"" + sets.list[i].name.substr(0,20) + "\"><br>";
	}	
	}
	document.getElementById("setList").innerHTML = res;
}

function loadSet(uid) {
	showLoading();
	setTimeout("confirmTestExists('" + uid + "')", 10);
}

function confirmTestExists(uid) {
	var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.SETEXISTS, {'uid':uid}));
	if (result == true) {
		setTimeout("renderSet('" + uid + "')", 10);
	} else {
		alert("Sorry that test no longer exists on the server.\n\nSomeone has removed it.\n\nReturning you to the home screen..");
		sets.deleteByUID(uid, false);
		homeScreen();
	}
}

function shareSet(uid) {
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "none";
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "none";
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";		
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var theSet = sets.getSetByUID(uid);
	
	var res = "<h1>Share Set: " + theSet.name + "</h1>"
	res += "<p>To share this set, copy and paste the following ID into another instance of Dreamunit, in the import screen.</p>"
	res += "<p>ID: <input type=\"text\" id=\"shareID\" value=\"" + uid + "\" class=\"bigBox\" size=\"20\" maxlength=\"25\"></p>"
	res += "<input type=\"button\" class=\"maskedButton\" value=\"OK\" onClick=\"renderSet('" + uid + "')\" >";
	
	var shArea = document.getElementById("shareArea");
	
	shArea.innerHTML = res;
	
	shArea.style.display = "block";	
	//shareArea
}

function renderSet(uid) {
	
	sets.selectSet(uid);
	
	// load the tests
	tests.loadForSet(uid);
	
	// render the set view
	var theSet = sets.getSetByUID(uid);
	var setArea = document.getElementById("setViewArea");
	var res = "";
	res += "<h1 style=\"margin:0px;padding:0px;\">" + theSet.name + "</h1>";
	res += "<p style=\"margin:0px;padding:0px; font-size:16px;\">ID: <span style=\"color:blue;\">" + uid + "</span><input type=\"image\" src=\"../Shared/du/images/globe.png\" onclick=\"shareSet('" + uid + "')\"> &nbsp;&nbsp; Created for <span style=\"color:blue;\">" + theSet.sitename + "</span></p>";
	res += "Options: <input type=\"button\" class=\"maskedButton\" value=\"Delete Set\" onClick=\"deleteSet('" + uid + "')\" > ";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"Update Set From Web\" onClick=\"updateSet('" + uid + "')\" > ";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"&lt;- Back\" onClick=\"homeScreen()\" ><br>";
	//res += "<select name=\"TestList\" style=\"width:100%;\" type=\"mmparameterlist\" columns=\"Name,SQL Data,Type,Direction,Default Value,Run-time Value\" size=\"6\"><option>val1,val2,val3,val4,val5,val6</option></select>";
	res += "<mm:treecontrol name=\"testList\" id=\"testList\" style=\"height:300px;width:100%\">"  
	res += "<MM:TREECOLUMN name=\"TestName\" width=\"230\" state=\"visible\" value=\"Test Name\"><MM:TREECOLUMN name=\"Column2\" width=\"260\" state=\"visible\" value=\"Applies to page\"><MM:TREECOLUMN name=\"uid\" state=\"hidden\" width=\"1\">";
	res += "<mm:treenode icon=\"1\" value=\"Tests\" state=\"expanded\">";
	/*res += "<mm:treenode value=\"tsfsd|sdfsdf\" state=\"expanded\"></mm:treenode>";
	res += "<mm:treenode value=\"tsfsdfsdfsd|sdfsdf\" state=\"expanded\"></mm:treenode>";
	res += "<mm:treenode value=\"tsfsd|sdfsdf\" state=\"expanded\"></mm:treenode>";	*/
	var rootfolder = site.siteRelativeToLocalPath("/");
	for (var i = 0; i < tests.list.length; i++) {
			var newfname = tests.list[i].appliesto.replace(rootfolder, "").replace(/:/g, "\\").replace("|", "").replace("|", "");	
			var newtname = tests.list[i].name.replace("|", "").replace("|", "");
			res += "<mm:treenode value=\"" + newtname + "|" + newfname + "|" + tests.list[i].uid + "\" state=\"expanded\"></mm:treenode>";
		}	
	res += "</mm:treenode>";
	res += "</mm:treecontrol>";
	res += "<input type=\"button\" class=\"maskedButton\" value=\"Edit Selected\" onClick=\"editSelectedTest()\" >  <input type=\"button\" class=\"maskedButton\" value=\"Delete Selected\" onClick=\"delSelectedTest()\" > ";
	
	res += "<input type=\"button\" class=\"maskedButton\" value=\"Create New Test\" onClick=\"createNewTestForSet('" + uid + "')\" > ";
	setArea.innerHTML = res;
	
	var bArea = document.getElementById("setupButtonArea");
	bArea.style.display = "none";
	var cArea = document.getElementById("createNewArea");
	cArea.style.display = "none";	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "none";	
	var shArea = document.getElementById("shareArea");
	shArea.style.display = "none";	
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "block";	
}

function deleteSet(uid) {
	if (confirm("Are you sure you want to delete this?") == true) {
		showLoading();
		setTimeout("_deleteSet('" + uid + "')", 10);
	}
}

function _deleteSet(uid) {
	sets.deleteByUID(uid);
	homeScreen();
}

function createNewTestForSet(uid) {
	showLoading();
	setTimeout("_createNewTestForSet('" + uid + "')", 10);	
}

function _createNewTestForSet(uid) {

	//var theSet = sets.getSetByUID(uid);
	var tUid = tests.createNew(uid,"Unnamed");
	setTimeout("editTestForSet('" + uid + "', '" + tUid + "')", 10);	
}
var siteFileList = [];
function editTestForSet(uid, tuid) {
	var sArea = document.getElementById("setViewArea");
	sArea.style.display = "none";	
	// get the current set
	var theSet = sets.getSetByUID(uid);
	var theTest = tests.getTestByUID(tuid);
	
	var res = "";
	res += "<h1 style=\"margin:0px;padding:0px;\">Editing Test For " + theSet.name.substr(0,15) + "..</h1>";
	res += "<p>Name: <input type=\"text\" name=\"testName\"  id=\"testName\" class=\"bigBox\" size=\"20\" maxlength=\"25\" value=\"" + theTest.name + "\" ></p>";
	var rootfolder = site.siteRelativeToLocalPath("/");
	var ftree = filetree.getFileTree(rootfolder);
	siteFileList = [];
	var sB = "<p>Page to apply to: <select name=\"pChooser\" id=\"pChooser\" class=\"bigBox\" style=\"width:300px\">";
	var validDocTypes = ".html,.htm,.php,.jsp,.asp,.aspx,.cfm,.cmfx".split(",");
	for (var i = 0; i < ftree.length;i++) {
		var isValid = false;
		for (var v = 0; v < validDocTypes.length;v++) {
			if (ftree[i].substr(ftree[i].length-validDocTypes[v].length) == validDocTypes[v])
				isValid = true;
		}
		var issel = "";

		if (isValid == true) {
			if (filetree.fileIsValidForTesting(ftree[i]) == true) {
				if (ftree[i] == theTest.appliesto)
					issel = " selected"				
				sB += "<option " + issel + ">" + ftree[i].replace(rootfolder, "").replace(/:/g, "\\") + "</option>";
				siteFileList[siteFileList.length] = ftree[i];
			}
		}
	}
	sB += "</select></p>";
	res += sB;
	res += "JavaScript for test:<br><textarea id=\"testContents\" style=\"width:100%;height:200px;font-family:courier new;font-size:10px;	color:#113311;\">" + theTest.js + "</textarea><br>";
	res += "<a href=\"#\" ><img src=\"../Shared/du/images/buttons/okbutton.png\" style=\"border:0px;\" onMouseUp=\" setTimeout('saveTest(\\'" + tuid + "\\')', 10)\"></a> <a href=\"#\" ><img src=\"../Shared/du/images/buttons/cancelbutton.png\" style=\"border:0px;\" onMouseUp=\"renderSet('" + uid + "')\"></a>";
	var ntArean = document.getElementById("createTestArea");
	ntArean.innerHTML = res;
	
	var wArea = document.getElementById("waitScreen");
	wArea.style.display = "none";
	var ntArea = document.getElementById("createTestArea");
	ntArea.style.display = "block";	
			
}

function saveTest(tuid) {
	var testName = document.getElementById("testName");
	testName.blur();
	var testJS = document.getElementById("testContents");
	testJS.blur();	
	var setnms = document.getElementById("pChooser");
	setnms.focus();	
	setTimeout("_saveTest('" + tuid + "');", 250);
}

function _saveTest(tuid) {
	var setnms = document.getElementById("pChooser");
	var si = 0;
	if (setnms.selectedIndex)
		si = setnms.selectedIndex;
	var targetPage = siteFileList[si];
	var testJS = document.getElementById("testContents").value;
	var testName = document.getElementById("testName").value;
	if (!targetPage || targetPage == "undefined" || targetPage.length < 1)
		targetPage = "NONE";
		
	var tIndex = tests.getTestIndexByUID(tuid);
	tests.list[tIndex].name = testName;
	tests.list[tIndex].js = testJS;
	tests.list[tIndex].appliesto = targetPage;
	var rootfolder = site.siteRelativeToLocalPath("/");
	tests.list[tIndex].appliestoroot = rootfolder;
	
	showLoading();
	setTimeout("_FsaveTest('" + tuid + "');", 250);
}

function _FsaveTest(tuid) {
	var tIndex = tests.getTestIndexByUID(tuid);
	tests.updateTest(tuid);
	renderSet(tests.list[tIndex].suid);
}

function editSelectedTest() {
	try {
	var sIndex = document.getElementById("testList").selectedNodes[0].value;
	var uid = sIndex.split("|")[2];	
	var tIndex = tests.getTestIndexByUID(uid);
	var suid = 	tests.list[tIndex].suid;
	editTestForSet(suid, uid);
	} catch(e) {
		alert("Please select a test.");
	}
}

function delSelectedTest() {
	var sIndex = document.getElementById("testList").selectedNodes[0].value;
	var uid = sIndex.split("|")[2];
	delTest(uid);
	
}
function delTest(tuid) {
	var tIndex = tests.getTestIndexByUID(tuid);
	var suid = tests.list[tIndex].suid;
	if (confirm('Are you sure you want to delete that test?') == true) {
		tests.deleteTestByUID(tuid);
		renderSet(suid);
	}
}

function updateSet(uid) {
	if (confirm('Are you sure you want to update from the web?') == true) {		
		showLoading();
		setTimeout("_updateSet('" + uid + "');", 250);	
	}
}

function _updateSet(uid) {
	tests.deserializeTestsFromWeb(uid);
	tests.serializeAndWriteTestsToFileSystem(uid);
	renderSet(uid);
}
</script>

</HEAD>
<BODY onload="init()">
 <form name="uorl" style="margin:0px; padding:0px;">
  <table border="0" class="mainContentTable" width="100%">
    <!--DWLayoutTable-->
    <tr> 
      <td valign="top" nowrap>
		<table border="0"  width="100%"><tr><td><img src="../Shared/du/images/dulogo.png"/><br><td><td align="right" valign="bottom"><input type="button" onClick="window.close()" value="Close"><br>(v1.0) - www.dreamunittest.com</td></tr></table><br />
		<table width="100%" ><tr><td width="180" valign="top">CONFIGURED TEST SETS<br><div id="setList" style="margin-top:10px"></div></td><td><img src="../Shared/du/images/separator.gif"></td><td style="padding-left:10px;"  valign="top"><div id="setupButtonArea" style="display:none;">
			<a href="#" ><img src="../Shared/du/images/buttons/createnewtestset.png" style="margin-left:10px;margin-top:130px; border:0px;" onMouseUp="createNewWizard()"></a><br /><br />
			<a href="#"><img src="../Shared/du/images/buttons/connecttoexisting.png" style="margin-left:10px;border:0px;" onMouseUp="importWizard()"></a>
			</div><div style="display:none; width:100%; height:100%" id="createNewArea">
				<h1>Create a New Test Set</h1>
				<p>A test set is a collection of tests for a particular site within Dreamweaver. You can create multiple sets for a particular site.</p>
				<p>Set name: <input type="text" name="setname" id="setname" class="bigBox" size="20" maxlength="25" value=""></p>
				<div id="siteChooser1">
				
				</div>
				<p><a href="#" ><img src="../Shared/du/images/buttons/okbutton.png" style="border:0px;" onMouseUp="createNewSet()"></a> <a href="#" ><img src="../Shared/du/images/buttons/cancelbutton.png" style="border:0px;" onMouseUp="homeScreen()"></a></p>
			</div><div style="display:none; width:100%; height:100%" id="importNewArea">
					<h1>Connect to an Existing Test Set</h1>
					<p>A test set is a collection of tests for a particular site within Dreamweaver. You must import the set and assign it to the correct site in your local copy of Dreamweaver.</p>
					<p>Set name: <input type="text" name="setname2" id="setname2" class="bigBox" size="20" maxlength="25" value=""></p>
					<p>Set ID: <input type="text" name="setidimport" id="setidimport" class="bigBox" size="20" maxlength="25" value=""></p>
					<div id="siteChooser2">

					</div>
					<p><a href="#" ><img src="../Shared/du/images/buttons/okbutton.png" style="border:0px;" onMouseUp="importSet()"></a> <a href="#" ><img src="../Shared/du/images/buttons/cancelbutton.png" style="border:0px;" onMouseUp="homeScreen()"></a></p>
				</div><div style="display:none;" id="waitScreen">
				<h1 style="margin-top:130px;margin-left:25px;">Please wait..</h1>
				</div><div style="display:none; width:100%; height:100%" id="setViewArea"></div><div style="display:none; width:100%; height:100%" id="createTestArea"></div><div style="display:none; width:100%; height:100%" id="shareArea"></div><div style="display:none; width:100%; height:100%" id="importSetNotExist"></div>
		</td></tr></table>
      </td>
    </tr>
  </table>
</form>
</BODY>
</HTML>
